﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TripsLogic.util;
using System.Data;
using TripsLogic.model;

namespace TripsLogic.controller
{
    public class AddressController
    {

        internal static DataTable getAddressByUserId(string userId)
        {
            //get
            try
            {
                object[,] parameters = { { "@userId", userId } };
                return DB.getData("select * from ADDRESS where userId=@userId", parameters);
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error getAddressByUserId"));
                return null;
            }
        }

        internal static bool deleteAddress(string gvIDs)
        {
            try
            {
                return DB.exec("delete from ADDRESS where id in " + gvIDs + ";");
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error deleteAddress"));
                return false;
            }
        }

        internal static bool updateAddress(Address address)
        {
            try
            {
                object[,] parameters =  {   { "@Address", address.address },
                                        { "@Id", address.id },
                                        { "@Alias", address.alias }
                                    };
                return DB.exec("update Address set address = @address, alias = @alias where Id = @Id", parameters);
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error updateAddress"));
                return false;
            }
        }

        internal static bool createAddress(Address address)
        {
            try
            {
                object[,] parameters =  {{"@Address", address.address }, 
                                         {"@UserId", address.userId },
                                        {"@Alias", address.alias }};
                return DB.exec("insert into ADDRESS (address, userid, alias) " +
                    "values (@address, @userid, @alias);", parameters);
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error createAddress"));
                return false;
            }
        }

        internal static string getCurrentId()
        {
            try
            {
                return DB.getValue("select IDENT_CURRENT('ADDRESS')");
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error getCurrentArticleId"));
                return "";
            }
        }
    }
}